package com.thoughtworks.android.androsphere.framework;

import java.util.Random;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import com.thoughtworks.android.androsphere.activity.MessageActivity;
import com.thoughtworks.android.androsphere.R;

public class CloudMessageNotifier {
    private Context context;
    private NotificationManager notificationManager;

    public CloudMessageNotifier(Context context) {
        this.context = context;
        notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    }

    public void postNotification(String tickerText, String expandedTitle, String expandedText) {
        notificationManager.notify(new Random().nextInt(), createNotification(tickerText, expandedTitle, expandedText));
    }

    private Notification createNotification(String tickerText, String title, String text) {
        long now = System.currentTimeMillis();

        Intent intent = new Intent(context, MessageActivity.class);
        intent.putExtra("title", title);
        intent.putExtra("text", text);

        intent.setAction("actionstring" + System.currentTimeMillis());

        PendingIntent launchIntent = PendingIntent.getActivity(context, 0, intent, 0);

        Notification notification = new Notification(R.drawable.messagesmall, tickerText, now);
        notification.setLatestEventInfo(context, title, text, launchIntent);

        return notification;
    }
}
